package com.longteng.lesson2.dubbo;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.alibaba.fastjson.JSON;
import com.longteng.lesson2.domain.User;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class GenericDubbo {

    @Test
    public void jsonTest(){
        User user=new User();
        user.setName("龙腾");
        String s = JSON.toJSONString(user);
        System.out.println(s);
    }
    @Test
    public void dubboTestServiceGenericTest(){
        //这里封装了一个dubboClient的方法，在里面初始化接口内容
        GenericService genericService=dubboClient("com.longteng.service.DubboTestService","longteng","1.0");
        String[] parameterTypes =new String[1];
        parameterTypes[0]="java.lang.String";
        Object[] args =new Object[1];
        args[0]="你好 dubbo";
        Object returnObject=genericService.$invoke("sayHello",parameterTypes,args);
        System.out.println(returnObject);
    }
    @Test
    public void userServiceGenericTest(){
        GenericService genericService= dubboClient("com.longteng.service.UserService","longteng","1.0");
        String[] parameterTypes =new String[1];
        parameterTypes[0]="com.longteng.domain.User";
        Map map=new HashMap();
        map.put("id",1);
        Object[] args =new Object[1];
        args[0]=map;
        Object returnObject =genericService.$invoke("getUser",parameterTypes,args);
        System.out.println(returnObject);
    }
    //这里面的一系列代码  = spring的xml配置
    public GenericService dubboClient(String interfaceName,String group,String version){
        ApplicationConfig application = new ApplicationConfig();
        application.setName("dubbo-consumer");
        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://47.105.55.243:20880");
        //--------------------和xml里面的连接注册中心配置是一样的，只不过是使用代码的方式-------------------------
        ReferenceConfig<GenericService> reference = new
                ReferenceConfig<GenericService>();
        reference.setApplication(application); //设置应用信息
        reference.setRegistry(registry); //设置注册中心连接的地址
        reference.setInterface(interfaceName);
        reference.setGeneric(true); // 声明为泛化接口
        reference.setGroup(group);
        reference.setVersion(version);
        reference.setUrl("dubbo://47.105.55.243:20880");


        return reference.get();
    }


    @Test
    public void t2(){
        ApplicationConfig application = new ApplicationConfig();
        application.setName("dubbo-consumer");
        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://47.105.55.243:20880");
        //--------------------和xml里面的连接注册中心配置是一样的，只不过是使用代码的方式-------------------------
        ReferenceConfig<GenericService> reference = new
                ReferenceConfig<GenericService>();
        reference.setApplication(application); //设置应用信息
        reference.setRegistry(registry); //设置注册中心连接的地址
        reference.setInterface("com.longteng.service.UserService");
        reference.setGeneric(true); // 声明为泛化接口
        reference.setGroup("longteng");
        reference.setVersion("1.0");
        reference.setUrl("dubbo://47.105.55.243:20880");
        //------------------配置接口信息，和xml里面配置接口是一样的，只不过是使用代码的方式--------------------------------------
        //从缓存里面拿接口
//        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
//        GenericService genericService = cache.get(reference);
        GenericService genericService =reference.get(); //相当于(UserService) context.getBean("userService");
        String[] parameterTypes =new String[1];
        parameterTypes[0]="com.longteng.domain.User";
//        com.longteng.domain.User user=new User();
//        user.setId(1);
        Map map=new HashMap();
        map.put("id",1);
        Object[] args =new Object[1];
        args[0]=map;
        args[1]="字符串参数";

        Object returnObject =genericService.$invoke("getUser",parameterTypes,args);
        System.out.println(returnObject);
    }
}
